var frmHeight = getClientHeight();
var frmWidth = getClientWidth();
var post={};
var tinggi = Math.round(frmHeight/2)-73;
	var	lebar = frmWidth-12;
	var	lebar2 = Math.round(lebar/2)-9.55;
function getClientHeight(){
	var theHeight;
	if (window.innerHeight)
		theHeight=window.innerHeight;
	else if (document.documentElement && document.documentElement.clientHeight) 
		theHeight=document.documentElement.clientHeight;
	else if (document.body) 
		theHeight=document.body.clientHeight;
	
	return theHeight;
}

function getClientWidth(){
	var theWidth;
	if (window.innerWidth) 
		theWidth=window.innerWidth;
	else if (document.documentElement && document.documentElement.clientWidth) 
		theWidth=document.documentElement.clientWidth;
	else if (document.body) 
		theWidth=document.body.clientWidth;

	return theWidth;
}

function showChangePass(){	
	$('#popup_password').window({
		modal: true,
		shadow: false,
		closed: true,
		collapsible: false,
		resizable: false,
		minimizable: false,
		maximizable: false,
		width: 400,
		height: 145,
		title: ' Mengganti Sandi'
	});	
	
	$('#passOld').val('');
	$('#passEdit').val('');
	$('#confPassEdit').val('');
	$('#popup_password').css('display','inline-block');
	$('#popup_password').window('open');
}

function toglePass2Text(obj){
	if(obj.checked){
		$('input#passOld')[0].type = 'text';
		$('input#passEdit')[0].type = 'text';
		$('input#confPassEdit')[0].type = 'text';
	}else{
		$('input#passOld')[0].type = 'password';
		$('input#passEdit')[0].type = 'password';
		$('input#confPassEdit')[0].type = 'password';
	};
}

$('#btChangePass').live('click', function changePass(){
	if(confirm('Sandi lama akan diganti dengan yang baru, apakah anda setuju?')){
		if($('#confPassEdit').val()==$('#passEdit').val()){
			_post = [{name:'oldPass', value: $('#passOld').val()},
					{name:'newPass', value: $('#confPassEdit').val()}]
			$.post(host+'dashboard/changePass', _post, function(resp) {
				if(resp=='sukses'){
					alert('Sandi lama anda telah diganti! aplikasi akan logoff secara otomatis.');
					window.location = host+'login/logout_na';
				} else {
					alert('Sandi lama yang anda masukan salah. Coba lagi!');
				}
			})
		} else {
			alert('Konfirmasi sandi baru harus sama dengan sandi baru!');
		}
	} else {
		$('#popup_password').window('close');
	}	
})

function showUpFoto(){
	$('#popup_upFoto').window({
		modal: true,
		shadow: false,
		closed: true,
		collapsible: false,
		resizable: false,
		minimizable: false,
		maximizable: false,
		width: 400,
		height: 145,
		title: ' Upload Photo'
	});	
	
	$('#fotoDest').val('');
	
	$('#popup_upFoto').window('open');
}

function ShowFolderList(folderspec)
{
   var fso, f, fc, s;
   fso = new ActiveXObject("Scripting.FileSystemObject");
   f = fso.GetFolder(folderspec);
   fc = new Enumerator(f.SubFolders);
   s = "";
   for (; !fc.atEnd(); fc.moveNext())
   {
      s += fc.item();
      s += "<br>";
   }
	alert(s);
} 

function klikThis(){
	$('#targetDiv').html("<div id='footer'><p style='float: right;'><a href='#' style='color: #BFEBFF; text-decoration: none;'>Hubungi Kami</a></p><p>Copyright &copy; 2012, Fathan Collection - All Rights Reserved</p></div>");
}

function fillCombo(url, SelID, value, value2, value3, value4){
	//if(Ext.get(SelID).innerHTML == "") return false;
	if (value == undefined) value = "";
	if (value2 == undefined) value2 = "";
	if (value3 == undefined) value3 = "";
	if (value4 == undefined) value4 = "";
	
	$.post(url, {"v": value, "v2": value2, "v3": value3, "v4": value4},function(data){
		$('#'+SelID).append(data);
	});

}

// JavaScript Document
function submit_form_na(frm,func){
	//$.blockUI({ overlayCSS: { backgroundColor: '#E0ECFF' } }); 
    var url = jQuery('#'+frm).attr("url");
    jQuery('#'+frm).form('submit',{
            url:url,
            onSubmit: function(){
                  return $(this).form('validate');
            },
            success:function(data){
				//$.unblockUI();
                if (func == undefined ){
                     if (data == "1"){
                        pesan('Data Sudah Disimpan ','Sukses');
                    }else{
                         pesan(data,'Result');
                    }
                }else{
                    func(data);
                }
            },
            error:function(data){
				//$.unblockUI();
                 if (func == undefined ){
                     pesan(data,'Error');
                }else{
                    func(data);
                }
            }
    });
}

function checkBoxClick(obj){ 
	//var data_id=[];
	
	var id=obj.id;
	
	if(obj.className == 'legend-show'){
		obj.className = 'legend-hide';
		
	} else {
		obj.className = 'legend-show';
		//console.log('ID = '+id+' KAT='+kat+' IDX='+idx);
	}	
	
};


function create_combo_na(id,url,val){
	for(i in id){
			 fillCombo(url+id[i]+'/'+val[i],id[i]);
	}
}

function create_number_na(id){
	for(i in id){
			 create_number(id[i]);
	}
}

function create_number(id){
	$('#'+id).numberbox({  
		min:-100
		
	});	
}
/*
var divcontainer;
function mywindow(txt,judul,width,height){
    
    divcontainer = "mywindow"+Math.floor(Math.random()*99999);
    $("<div id='"+divcontainer+"'></div>").appendTo("body");
    divcontainer = "#"+divcontainer;
    $(divcontainer).css({'padding':'10px'});
    $(divcontainer).html(txt);

    if (judul === undefined ) judul ='DATA';
   /* if (width === undefined ) {
        var wdh = $(divcontainer).outerWidth(true);
        if ( wdh > 750 ) wdh = 700;
        if ( wdh < 250 ) wdh = 500;
        width = wdh ;
    }
    if (height === undefined ){
        var hgt = $(divcontainer).outerHeight(true);
        if ( hgt > 550 ) hgt = 550;
        if ( hgt < 150 ) hgt = 150;
        height = hgt ;
    }
	
    var leftCenter = window.innerWidth/2;
	var topCenter = window.innerHeight/2;
    var panelLeft = leftCenter - (width/2);
    var panelTop = topCenter - (height/2);
    $(divcontainer).css({'opacity':0});
	$(divcontainer).window({
                            title: judul,
                            left:panelLeft,
                            top:panelTop,
                            width:width,
                            height:height,
                            shadow: false,
                            closed: true,
							modal:true,
                            collapsible:false,
                            collapsed:false,
                            onBeforeClose:function(){
								
								$($(divcontainer).window('panel')).animate({
									"width": "-15px",
									"height":"-15px",
									"left":  leftCenter +"px",
									"top": topCenter +"px",
									"opacity":0
								  }, 777,function(){
									  $(divcontainer).html('');
									 
										$($(divcontainer).window('panel')).css({'left':panelLeft+'px','top':panelTop+'px','width':width+'px','height':height+'px'});
										
										$(divcontainer).window("close",true);
									    $(divcontainer).window("destroy",true);
									  return true;
									  });
								
							},
							onBeforeOpen:function(){
									
								$($(divcontainer).window('panel')).css({'left':leftCenter+'px','top':topCenter+'px','width':'1px','height':'1px'});
							},
							onOpen:function(){
								/* $($(divcontainer).window('panel')).animate({
									"width": (width-10)+"px",
									"height":(height+45)+"px",
									"left": panelLeft+"px",
									"top": panelTop+"px",
									'opacity':1
								  }, 777,function(){
									  $(divcontainer).animate({'width':(width-35)+'px','height':(height)+'px','opacity':1}
									  ,777,function(){
										   //$(divcontainer).window('resize');
										  });
									  }); 
							}
							
                            });
  $(divcontainer).window('open');
   $(divcontainer).window('open');
}

function mywindow_close(){
	$(divcontainer).window('close');
}
*/

var divcontainer;
function mywindow(txt,judul,width,height){
    divcontainer = "mywindow"+Math.floor(Math.random()*99999);
    $("<div id='"+divcontainer+"'></div>").appendTo("body");
    divcontainer = "#"+divcontainer;
    $(divcontainer).css({'padding':'10px'});
    $(divcontainer).html(txt);

    if (judul === undefined ) judul ='DATA';
    if (width === undefined ) {
        var wdh = $(divcontainer).outerWidth(true);
        if ( wdh > 750 ) wdh = 750;
        if ( wdh < 250 ) wdh = 250;
        width = wdh ;
    }
    if (height === undefined ){
        var hgt = $(divcontainer).outerHeight(true);
        if ( hgt > 550 ) hgt = 550;
        if ( hgt < 150 ) hgt = 150;
        height = hgt ;
    }
    var leftCenter = window.innerWidth/2;
	var topCenter = window.innerHeight/2;
    var panelLeft = leftCenter - (width/2);
    var panelTop = topCenter - (height/2);
    $(divcontainer).css({'opacity':0});
	$(divcontainer).window({
                            title: judul,
                            left:panelLeft,
                            top:panelTop,
                            width:width,
                            height:height,
							modal:true,
                            shadow: false,
                            closed: true,
                            collapsible:false,
                            collapsed:false,
                            onBeforeClose:function(){
								
								$($(divcontainer).window('panel')).animate({
									"width": "-15px",
									"height":"-15px",
									"left":  leftCenter +"px",
									"top": topCenter +"px",
									"opacity":0
								  }, 777,function(){
									  $(divcontainer).html('');
									 
										$($(divcontainer).window('panel')).css({'left':panelLeft+'px','top':panelTop+'px','width':width+'px','height':height+'px'});
										
										$(divcontainer).window("close",true);
									    $(divcontainer).window("destroy",true);
									  return true;
									  });
								
							},
							onBeforeOpen:function(){
									
								$($(divcontainer).window('panel')).css({'left':leftCenter+'px','top':topCenter+'px','width':'1px','height':'1px'});
							},
							onOpen:function(){
								 $($(divcontainer).window('panel')).animate({
									"width": (width-10)+"px",
									"height":(height+45)+"px",
									"left": panelLeft+"px",
									"top": panelTop+"px",
									'opacity':1
								  }, 777,function(){
									  $(divcontainer).animate({'width':(width-35)+'px','height':(height)+'px','opacity':1}
									  ,777,function(){
										   //$(divcontainer).window('resize');
										  });
									  });
							}
		
        });
  $(divcontainer).window('open');
   $(divcontainer).window('open');
}

function mywindow_close(){
	$(divcontainer).window('close');
}



function get_form(url,post,w,h,title){
	$.post(url,post,function(r){
		mywindow(r,title,w,h);	
	});
		
}

function yDownload(url,params,status){
  if(params==undefined) return window.location.href=url;
  if(status==undefined) status="";

  var newWindow = window.open(url,"_blank",status,true);// 'WinPost'+Math.floor(Math.random()*99999));
  var html = "";
  html += "<form  id='formid' method='post' action='" + url + "'>";

  $.each(params, function(key, value) {

    if (value instanceof Array || value instanceof Object) {
      $.each(value, function(key1, value1) {
        html += "<input type='hidden' name='" + key + "["+key1+"]' value='" + value1 + "'/>";
      });
    }else{
      html += "<input type='hidden' name='" + key + "' value='" + value + "'/>";
    }
  });

  html += "</form><script type='text/javascript'>document.getElementById(\"formid\").submit();</script>";
  newWindow.document.write(html);
}

function openWindowWithPost(url,params){
  var newWindow = window.open(url,"_blank",'',true);// 'WinPost'+Math.floor(Math.random()*99999));
  var html = "";
  html += "<form  id='formid' method='post' action='" + url + "'>";

  $.each(params, function(key, value) {

    if (value instanceof Array || value instanceof Object) {
      $.each(value, function(key1, value1) {
        html += "<input type='hidden' name='" + key + "["+key1+"]' value='" + value1 + "'/>";
      });
    }else{
      html += "<input type='hidden' name='" + key + "' value='" + value + "'/>";
    }
  });

  html += "</form><script type='text/javascript'>document.getElementById(\"formid\").submit();</script>";
  newWindow.document.write(html);
}


function clear_form_na(ele) {

  $('#'+ele).find(':input').each(function() {
    switch(this.type) {
      case 'password':
      case 'select-multiple':
      case 'select-one':
      case 'text':
      case 'textarea':
        $(this).val('');
        break;
      case 'checkbox':
      case 'radio':
        this.checked = false;
    }
  });

}

function add_koma(nStr) {
  nStr += '';
  x = nStr.split(',');
  x1 = x[0];
  x2 = x.length > 1 ? ',' + x[1] : '';
  var rgx = /(\d+)(\d{3})/;
  
  while (rgx.test(x1)) {
    x1 = x1.replace(rgx, '$1' + ',' + '$2');
  }
  return x1 + x2;
}

function get_chart(id,type_chart,table,render_id,w,h){
	
	$.post(host+'dashboard/get_datagrap',post,function(r){
			//r = eval('('+r+')');
			id = new FusionCharts(host+"assets/swf/"+type_chart+".swf", id, w, h);
			id.setDataXML(r);
			id.render(render_id);
	});
}

